#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tornado.web import RequestHandler
import pymongo
import re
import render
import codecs
from _base import BaseHandler
import datetime

_encoding = 'utf-8'
_errors='ignore'
color = ['AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE','AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE','AFD8F8','F6BD0F','8BBA00','FF8E46','008E8E','D64646','8E468E','588526','B3AA00','008ED6','9D080D','A186BE',]
client_pid = {}

class PV(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _platform = self.get_argument('platform', '').strip()
        if not _time:
            _time = '201110'
        if not _platform:
            _platform = 'all'
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find()

        print dir(posts)
        _posts = []
        pv = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        for post in posts:
            pid = post['pid']
            if pid == '127f7a5e3a366bd0' or pid == '53ef315e3bad9e03' or pid == 'a8c022bcd2adcbee':
                continue
            if _platform == 'android':
                print post.get('keyword','')
                datamatch = compiles_android.search(post.get('keyword',''))
                if datamatch:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            elif _platform == 'ios':
                datamatch = compiles_ios.search(post.get('keyword',''))
                if datamatch:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            elif _platform == 'wap':
                if 'wap' == post['server']:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            else:
                pv[post['date']] = pv.get(post['date'], 0) + post['pv']
           
        print pv
#        for a in pv.items():
#            print dir(a)
        lpv = list(pv)
        lpv.sort()
        
        #xmlStr = render.xml(pv)
        xml_head=ur"<graph caption=' {0}年{1}月 PV数据报表图' xAxisName='Day' yAxisName='PV' \
        showValues='1' lineThickness='1' animation='1' alpha='100' showLimits='1' decimalPrecision='1' rotateNames='1' numDivLines='3'\
        bgcolor='F3f3f3' bgAlpha='70' \
        showColumnShadow='0' divlinecolor='c5c5c5' divLineAlpha='80' \
        showAlternateHGridColor='1' alternateHGridColor='f8f8f8' alternateHGridAlpha='80' > ".format(_time[:4], _time[4:])
        xml_content = ''
        for i in range(len(lpv)):
            xml_content += "<set name='{0}' value='{1}' color='{2}'/>".format(lpv[i], pv[lpv[i]], color[i])
        xml_footer = '''</graph>'''
        xmlStr = xml_head+xml_content+xml_footer
        
        self.render('templates/client-pv.html', content=pv, keys=lpv, add=add, xmlStr=xmlStr, time=_time, platform=_platform )

class UV(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _platform = self.get_argument('platform', '').strip()
        if not _time:
            _time = '201111'
        if not _platform:
            _platform = 'all'
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find()

        print dir(posts)
        _posts = []
        uv1 = {}
        uv2 = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        compiles_other = re.compile('^((?!android|ios|iphone|ipad|wap).)*$', re.I)
        for post in posts:
            pid = post['pid']
            
            if _platform == 'android':
                datamatch = compiles_android.search(post.get('keyword',''))
                if datamatch:
                    uv1[post['date']] = uv1.get(post['date'], 0) + int(post.get('uv1',0))
                    uv2[post['date']] = uv2.get(post['date'], 0) + int(int(post.get('vv1',0)))
            elif _platform == 'ios':
                datamatch = compiles_ios.search(post.get('keyword',''))
                if datamatch:
                    uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                    uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
            elif _platform == 'wap':
                if 'wap' in post.get('keyword',''):
                    uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                    uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
            elif _platform == 'other':
                keyword = post.get('keyword','')
                datamatch = compiles_other.search(keyword)
                if not keyword  or datamatch:
                    print 'keyword:{0}, pid:{1}, uv1:{2}, uv2:{3}, pv:{4}'.format(keyword, post['pid'], post.get('uv1',0), post.get('uv2',0), int(post.get('uv1',0)))
                    uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                    uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
            else:
                uv1[post['date']] = uv1.get(post['date'], 0) + post.get('uv1',0)
                uv2[post['date']] = uv2.get(post['date'], 0) + post.get('uv2',0)
           
        print uv1
        print uv2

        luv1 = list(uv1)
        luv1.sort()
        
        luv2 = list(uv2)
        luv2.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 UV数据报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(luv1)):
            xml_categories += "<category Name='{0}' />".format((luv1[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='非UV2.0客户端' color='8BBA00'>"
        for i in range(len(luv1)):
            xml_dataset1 += "<set value='{0}' />".format(uv1[luv1[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='UV2.0客户端' color='8E468E'>"
        for i in range(len(luv2)):
            xml_dataset2 += "<set value='{0}' />".format(uv2[luv2[i]])
        xml_dataset2 += "</dataset>"
        
        xml_trendlines = ur"<trendlines>\
        <line startValue='1200000' color='91C728' displayValue='Target' showOnTop='1'/>\
        </trendlines>"
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_trendlines+xml_footer
        
        self.render('templates/client-uv.html', uv1=uv1, uv2=uv2, keys=luv1, add=add, xmlStr=xmlStr, time=_time, platform=_platform )

class VV(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _platform = self.get_argument('platform', '').strip()
        if not _time:
            _time = '201111'
        if not _platform:
            _platform = 'all'
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find()

        print dir(posts)
        _posts = []
        android_vv2 = {}
        ios_vv2 = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        for post in posts:
            pid = post['pid']
            
            datamatch_android = compiles_android.search(post.get('keyword',''))
            datamatch_ios = compiles_ios.search(post.get('keyword',''))
            
            if datamatch_android:
                android_vv2[post['date']] = android_vv2.get(post['date'], 0) + post.get('vv2',0)
            elif datamatch_ios:
                ios_vv2[post['date']] = ios_vv2.get(post['date'], 0) + post.get('vv2',0)
            else:
                pass

        

        lvv_ios = list(ios_vv2)
        lvv_ios.sort()
        
        lvv_android = list(android_vv2)
        lvv_android.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 2.0客户端 VV数据报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(lvv_ios)):
            xml_categories += "<category Name='{0}' />".format((lvv_ios[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='IOS平台vv' color='8BBA00'>"
        for i in range(len(lvv_ios)):
            xml_dataset1 += "<set value='{0}' />".format(ios_vv2[lvv_ios[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='Android平台vv' color='8E468E'>"
        for i in range(len(lvv_android)):
            xml_dataset2 += "<set value='{0}' />".format(android_vv2[lvv_android[i]])
        xml_dataset2 += "</dataset>"
        
        xml_trendlines = ur"<trendlines>\
        <line startValue='1000000' color='91C728' displayValue='Target' showOnTop='1'/>\
        </trendlines>"
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_trendlines+xml_footer
        
        self.render('templates/client-vv.html', android_vv2=android_vv2, ios_vv2=ios_vv2, keys=lvv_ios, xmlStr=xmlStr, time=_time, platform=_platform )

class PID(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _pid = self.get_argument('pid', '').strip()
        
        read_pid()
        print client_pid
        if not _pid:            
            self.render('templates/client-pid.html', client_pid=client_pid, pid='', time=_time, pv={}, uv={}, vv={}, xmlStr='')
            return
        if not _time:
            _time = '201111'
        
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        posts = collection.find({'pid':_pid}) 
        
        _posts = []
        pv = {}
        uv = {}
        vv = {}
        for post in posts:
            _posts.append( post )
            pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            uv[post['date']] = uv.get(post['date'], 0) + post.get('uv1',0) + post.get('uv2',0)
            vv[post['date']] = vv.get(post['date'], 0) + post.get('vv2',0)
        
        pid_list = list(pv)
        pid_list.sort()
        
        xml_head=ur"<graph caption=' {0}年{1}月 渠道数据报表图' xAxisName='Day' yAxisName='UV' showValues='1' showLabels='1'\
        numberPrefix=''> ".format(_time[:4], _time[4:])
        xml_categories = '<categories>'
        for i in range(len(pid_list)):
            xml_categories += "<category Name='{0}' />".format((pid_list[i])[6:])
        xml_categories += '</categories>'
        
        xml_dataset1 = ur"<dataset seriesName='pv' color='8BBA00'>"
        for i in range(len(pid_list)):
            xml_dataset1 += "<set value='{0}' />".format(pv[pid_list[i]])
        xml_dataset1 += "</dataset>"
        
        xml_dataset2 = ur"<dataset seriesName='uv' color='8E468E'>"
        for i in range(len(pid_list)):
            xml_dataset2 += "<set value='{0}' />".format(uv[pid_list[i]])
        xml_dataset2 += "</dataset>"
        
        xml_dataset3 = ur"<dataset seriesName='vv' color='A186BE'>"
        for i in range(len(pid_list)):
            xml_dataset3 += "<set value='{0}' />".format(vv[pid_list[i]])
        xml_dataset3 += "</dataset>"
        
        
        xml_footer = '</graph>'
        xmlStr = xml_head+xml_categories+xml_dataset1+xml_dataset2+xml_dataset3+xml_footer
        self.render('templates/client-pid.html', client_pid=client_pid, pid=_pid, keys=pid_list, time=_time, pv=pv, uv=uv, vv=vv, xmlStr=xmlStr)

class Test(BaseHandler):
    def get(self):
        result = {'status': "success", 'code': 1}
        self.write(result)
        self.finish()

class Daily(BaseHandler):
    def get(self): 
        _time = self.get_argument('time', '20111101').strip()
        _pd = self.get_argument('pd','').strip()
        _type = self.get_argument('type', 'pv').strip()
        
        st_time = datetime.datetime.strptime(_time, '%Y%m%d')
        if not _pd:
            ed_time = datetime.datetime.now()            
            delta = ed_time - st_time 
            _pd =  abs(delta.days -1)
            
        _pd = int(_pd)
        
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time[:6]]
        result = []
        for i in range(_pd) :
            results = {}
            dataresult = []
            datestr = st_time.strftime('%Y%m%d')
            
            results["date"] = st_time.strftime('%Y/%m/%d')
            posts = collection.find({'date':datestr})
            
            
            android_json = {}
            ios_json = {}
            other_json = {}
            wap_json = {}
            
            compiles_android = re.compile('android', re.I)
            compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
            compiles_other = re.compile('^((?!android|ios|iphone|ipad|wap).)*$', re.I)
            
            
            for post in posts:
                keyword = post.get('keyword','')
                print keyword
                if  compiles_android.search(keyword):
                    print '======android'
                    android_json['os'] = "android"
                    android_json['pv'] = android_json.get('pv', 0) + int(post.get('pv',0))
                    android_json['uv'] = android_json.get('uv', 0) + int(post.get('uv1',0)) + int(post.get('uv2',0))
                    android_json['uv1'] = android_json.get('uv1', 0) + int(post.get('uv2',0))
                    android_json['uv2'] = android_json.get('uv2', 0) + int(post.get('uv2',0))
                    android_json['vv'] = android_json.get('vv', 0) + int(post.get('vv1',0)) + int(post.get('vv2',0))
                    android_json['vv1'] = android_json.get('vv1', 0) + int(post.get('vv2',0)) 
                    android_json['vv2'] = android_json.get('vv2', 0) + int(post.get('vv2',0))
                elif compiles_ios.search(keyword):
                    print '======ios'
                    ios_json['os'] = "ios"
                    ios_json['pv'] = ios_json.get('pv', 0) + int(post.get('pv',0))
                    ios_json['uv'] = ios_json.get('uv', 0) + int(int(post.get('uv1',0))) + int(post.get('uv2',0))
                    ios_json['uv1'] = ios_json.get('uv1', 0) + int(post.get('uv1',0))
                    ios_json['uv2'] = ios_json.get('uv2', 0) + int(post.get('uv2',0))
                    ios_json['vv'] = ios_json.get('vv', 0) + int(post.get('vv1',0)) + int(post.get('vv2',0))
                    ios_json['vv1'] = ios_json.get('vv1', 0) + int(post.get('vv1',0)) 
                    ios_json['vv2'] = ios_json.get('vv2', 0) + int(post.get('vv2',0))
                elif 'wap' == keyword:
                    print '=======wap'
                    wap_json['os'] = "wap"
                    wap_json['pv'] = wap_json.get('pv', 0) + int(post.get('pv',0))
                    wap_json['uv'] = wap_json.get('uv', 0) + int(post.get('uv1',0)) + int(post.get('uv2',0))
                    wap_json['uv1'] = wap_json.get('uv1', 0) + int(post.get('uv1',0))
                    wap_json['uv2'] = wap_json.get('uv2', 0) + int(post.get('uv2',0))
                    wap_json['vv'] = wap_json.get('vv', 0) + int(post.get('vv1',0)) + int(post.get('vv2',0))
                    wap_json['vv1'] = wap_json.get('vv1', 0) + int(post.get('vv1',0)) 
                    wap_json['vv2'] = wap_json.get('vv2', 0) + int(post.get('vv2',0))
                elif compiles_other.search(keyword):
                    print '======other'
                    other_json['os'] = "other"
                    other_json['pv'] = other_json.get('pv', 0) + int(post.get('pv',0))
                    other_json['uv'] = other_json.get('uv', 0) + int(post.get('uv1',0)) + int(post.get('uv2',0))
                    other_json['uv1'] = other_json.get('uv1', 0) + int(post.get('uv1',0))
                    other_json['uv2'] = other_json.get('uv2', 0) + int(post.get('uv2',0))
                    other_json['vv'] = other_json.get('vv', 0) + int(post.get('vv1',0)) + int(post.get('vv2',0))
                    other_json['vv1'] = other_json.get('vv1', 0) + int(post.get('vv1',0)) 
                    other_json['vv2'] = other_json.get('vv2', 0) + int(post.get('vv2',0))
                else:
                    pass
            
            dataresult.append(android_json)
            dataresult.append(ios_json)
            dataresult.append(wap_json)
            dataresult.append(other_json)
            
            results["results"] = dataresult
            
            result.append(results)

            st_time = st_time + datetime.timedelta(days=1)
        
        
        self.write({"status":"success", "items":result})
        self.finish()
                
        

class ClientStatWAP(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        if not _time:
            _time = '201110'
        connection = pymongo.Connection("10.103.13.15", 27017)
        db = connection.statictisnew
        collection = db[_time]
        posts = collection.find().sort('date', pymongo.ASCENDING)
        
        _posts = []
        for post in posts:
            _posts.append( post )
        
        self.render('templates/clientstatictis-wap.html', content=_posts)

class SERVERPV(RequestHandler):
    def get(self): 
        _time = self.get_argument('time', '').strip()
        _platform = self.get_argument('platform', '').strip()
        _server = self.get_argument('server', '').strip()
        if not _time:
            _time = '201110'
        if not _platform:
            _platform = 'all'
        connection = pymongo.Connection("10.103.13.42", 27017)
        db = connection.DailyLog
        collection = db[_time]
        if not _server:
            posts = collection.find()
        else:
            posts = collection.find({'server' : _server})

        print dir(posts)
        _posts = []
        pv = {}
        compiles_android = re.compile('android', re.I)
        compiles_ios = re.compile('(ios|iphone|ipad)', re.I)
        for post in posts:
            pid = post['pid']
            if _platform == 'android':
                print post.get('keyword','')
                datamatch = compiles_android.search(post.get('keyword',''))
                if datamatch:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            elif _platform == 'ios':
                datamatch = compiles_ios.search(post.get('keyword',''))
                if datamatch:
                    pv[post['date']] = pv.get(post['date'], 0) + post['pv']
            else:
                pv[post['date']] = pv.get(post['date'], 0) + post['pv']
           
        print pv
#        for a in pv.items():
#            print dir(a)
        lpv = list(pv)
        lpv.sort()
        
        #xmlStr = render.xml(pv)
        xml_head=ur"<graph caption=' {0} PV数据报表图' xAxisName='Day' yAxisName='PV' \
        showValues='1' lineThickness='1' animation='1' alpha='100' showLimits='1' decimalPrecision='1' rotateNames='1' numDivLines='3'\
        bgcolor='F3f3f3' bgAlpha='70' \
        showColumnShadow='0' divlinecolor='c5c5c5' divLineAlpha='80' \
        showAlternateHGridColor='1' alternateHGridColor='f8f8f8' alternateHGridAlpha='80' > ".format(_time)
        xml_content = ''
        for i in range(len(lpv)):
            xml_content += "<set name='{0}' value='{1}' color='{2}'/>".format(lpv[i], pv[lpv[i]], color[i])
        xml_footer = '''</graph>'''
        xmlStr = xml_head+xml_content+xml_footer
        
        self.render('templates/client-server-pv.html', content=pv, keys=lpv, add=add, xmlStr=xmlStr)
        
def read_pid():
    global client_pid
    f = codecs.open('../LogTree/client-pid', 'r', encoding=_encoding, errors=_errors)
    row = f.readline()
    while row:
        _row = row.strip().split('^_^')
        if len(_row) <2 :
            row= f.readline()
            continue
        client_pid[_row[0]] = _row[1]
        row = f.readline()